package com.example.swagger;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.swagger.pojo.Api;
import com.example.swagger.pojo.SwParameter;
import com.example.swagger.pojo.SwResponseParameter;
import junit.framework.TestCase;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 描述：
 *
 * @author zhangxh
 * @since Created on 2021/8/9.
 */
public class ApiDocGenerator extends TestCase {

    public void test() {

        Properties properties = new Properties();
        properties.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(properties);
        StringWriter sw = new StringWriter();
        VelocityContext ctx = new VelocityContext();
        ctx.put("apiName", "接口名");
        ctx.put("apis", getApis());
        Template tpl = Velocity.getTemplate("template/api.md.vm");
        tpl.merge(ctx, sw);
        System.out.println(sw.toString());

    }

    public List<Api> getApis(){
        String doc = HttpUtil.get("http://localhost:8001/v2/api-docs");
        System.out.println(doc);
        JSONObject object = JSONObject.parseObject(doc);
        JSONObject paths = object.getJSONObject("paths");
        List<Api> apis = new ArrayList<>();
        JSONObject definitions = object.getJSONObject("definitions");
        paths.keySet().forEach(path->{
            Api api = new Api();
            api.setUrl(path);
            JSONObject meth = paths.getJSONObject(path);
            String methName = "";
            if (meth.containsKey("post")) {
                api.setMethod("POST");
                methName = "post";
            }
            if (meth.containsKey("get")) {
                api.setMethod("GET");
                methName = "get";
            }
            JSONObject methInfo = meth.getJSONObject(methName);
            List<SwParameter> parameters = new ArrayList<>();
            JSONArray array = methInfo.getJSONArray("parameters");
            array.forEach(p->{
                parameters.add(JSONObject.parseObject(p.toString(), SwParameter.class));
            });
            api.setParams(parameters);
            api.setDesc(methInfo.getJSONArray("tags").toString() + methInfo.getString("summary"));
            String responts = methInfo.getJSONObject("responses").getJSONObject("200").getJSONObject("schema").getString("$ref");
            responts = responts.replace("#/definitions/", "");
            String tObject = responts.replace("MxBaseRes«", "").replace("PageList«", "").replace("<", "").replace("", "");
            JSONObject resObj = definitions.getJSONObject(tObject);
            if (resObj != null) {
                JSONObject properties = resObj.getJSONObject("properties");
                List<SwResponseParameter> responseParameters = new ArrayList<>();
                properties.keySet().forEach(res -> {
                    SwResponseParameter responseParameter = new SwResponseParameter();
                    responseParameter.setDescription(properties.getJSONObject(res).getString("description"));
                    responseParameter.setName(res);
                    responseParameter.setType(properties.getJSONObject(res).getString("type"));
                    responseParameters.add(responseParameter);
                });
            }
            apis.add(api);
        });
        System.out.println(JSONObject.toJSONString(apis));
        return apis;
    }


}
